﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class ReplacementController : DefaultController
    {
        ReplacementService replacementService;

        public ReplacementController()
        {
            replacementService = new ReplacementService();
        }

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult List(int? index, string sortColumn, bool? sortAscending, int? pageSize, string shelfLocation, string rowLocation, string columnLocation,
            string name, string code, int? replacementBrandID, int? stockFrom, int? stockTo, int? minimumStock, decimal? publicPriceFrom, decimal? publicPriceTo)
        {
            int TotalRows;

            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;
            sortAscending = sortAscending ?? true;
            index = index ?? 0;
            replacementBrandID = replacementBrandID ?? 0;
            stockFrom = stockFrom ?? 0;
            stockTo = stockTo ?? 0;
            minimumStock = minimumStock ?? 0;
            publicPriceFrom = publicPriceFrom ?? 0;
            publicPriceTo = publicPriceTo ?? 0;
            if (string.IsNullOrEmpty(sortColumn))
                sortColumn = "Name";
            if (!pageSize.HasValue)
                pageSize = 5;

            ViewData["PageSizeFilter"] = new SelectList(new int[] { 5, 10, 15, 20 });
            ViewData["ShelfLocation"] = shelfLocation;
            ViewData["RowLocation"] = rowLocation;
            ViewData["ColumnLocation"] = columnLocation;
            ViewData["Name"] = name;
            ViewData["Code"] = code;
            ViewData["ReplacementBrandID"] = replacementBrandID;
            ViewData["StockFrom"] = stockFrom;
            ViewData["StockTo"] = stockTo;
            ViewData["MinimumStock"] = minimumStock;
            ViewData["PublicPriceFrom"] = publicPriceFrom;
            ViewData["PublicPriceTo"] = publicPriceTo;

            return PartialView(
                    new GenericGridList<Replacement>(
                        replacementService.GetPaginatedByFilter(CurrentBusinessUnitID, index.Value, pageSize.Value, sortColumn, sortAscending.Value, shelfLocation,
                        rowLocation, columnLocation, name, code, replacementBrandID.Value, stockFrom.Value, stockTo.Value, minimumStock.Value, publicPriceFrom.Value,
                        publicPriceTo.Value, out TotalRows), index.Value, sortColumn, pageSize.Value, TotalRows, sortAscending.Value));
        }

        [HttpGet]
        public PartialViewResult Filter()
        {
            ReplacementBrandService ReplacementBrandService = new ReplacementBrandService();
            ViewData["ReplacementBrandListFilter"] = new SelectList(ReplacementBrandService.GetAllByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID), "BrandId", "BrandName");
            return PartialView(new FilterModel() { PageSize = 5 });
        }

        #region CREATE
        [HttpGet]
        public PartialViewResult Create()
        {
            ReplacementBrandService ReplacementBrandService = new ReplacementBrandService();
            ProviderService ProviderService = new ProviderService();
            ViewData["BrandList"] = new SelectList(ReplacementBrandService.GetAllByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID), "BrandID", "BrandName", null);
            ViewData["ProviderList"] = new SelectList(ProviderService.GetAllByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID), "ProviderID", "Name", null);
            ViewData["PoundPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].PoundPrice.ToString();
            ViewData["DollarPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].DollarPrice.ToString();
            ViewData["ImportPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].ImportPrice.ToString();
            return PartialView("Create");
        }

        [HttpPost]
        public JsonResult Create(Replacement replacement)
        {
            JsonResult Response;

            replacement.BusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            replacementService.AddReplacement(replacement);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            Response.Data = replacement.ReplacementID > 0;

            return Json(Response);
        }
        #endregion

        #region EDIT
        [HttpGet]
        public PartialViewResult Edit(int? id)
        {
            if (id.HasValue)
            {
                ReplacementBrandService ReplacementBrandService = new ReplacementBrandService();
                ProviderService ProviderService = new ProviderService();
                Replacement selectedReplacement = replacementService.GetReplacementByID(id.Value);
                ViewData["BrandList"] = new SelectList(ReplacementBrandService.GetAllByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID), "BrandID", "BrandName", selectedReplacement.ReplacementBrandID);
                ViewData["ProviderList"] = new SelectList(ProviderService.GetAllByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID), "ProviderID", "Name", selectedReplacement.ProviderID);
                //List<SelectListItem> Currencies = new List<SelectListItem>();
                //Currencies.Add(new SelectListItem { Selected = true, Text = "$", Value = "ARG" });
                //Currencies.Add(new SelectListItem { Selected = false, Text = "U$S", Value = "USD" });
                //ViewData["CurrencyList"] = new SelectList(Currencies, "Value", "Text", selectedReplacement.Currency);
                ViewData["PoundPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].PoundPrice.ToString();
                ViewData["DollarPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].DollarPrice.ToString();
                ViewData["ImportPrice"] = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].ImportPrice.ToString();
                return PartialView(selectedReplacement);
            }
            else
            {
                return null;
            }
        }

        [HttpPost]
        public JsonResult Edit(Replacement replacement)
        {
            Replacement OriginalReplacement;
            JsonResult Response;

            OriginalReplacement = replacementService.GetReplacementByID(replacement.ReplacementID);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (OriginalReplacement != null)
            {
                TryUpdateModel<Replacement>(OriginalReplacement);

                replacementService.ModifyReplacement(OriginalReplacement);

                Response.Data = true;
            }
            else
            {
                Response.Data = false;
            }

            return Json(Response);
        }

        #endregion

        public JsonResult Delete(int? id)
        {
            JsonResult JsonResponse;
            Entities.Replacement DeleteReplacement;
            Dictionary<string, object> ResultDetails;

            ResultDetails = new Dictionary<string, object>();

            ResultDetails.Add("Message", string.Empty);
            ResultDetails.Add("Success", string.Empty);

            JsonResponse = new JsonResult();

            JsonResponse.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (id.HasValue)
            {
                DeleteReplacement = replacementService.GetReplacementByIDWithWO(id.Value);

                if (DeleteReplacement != null)
                {
                    if (DeleteReplacement.WorkOrderReplacements.Count > 0)
                    {
                        ResultDetails["Success"] = false;
                        ResultDetails["Message"] = Resources.Replacement.Index.ConfirmDeleteWithChildsMessage;
                    }
                    else
                    {
                        if (replacementService.DeleteReplacement(DeleteReplacement))
                        {
                            ResultDetails["Success"] = true;
                        }
                        else
                        {
                            ResultDetails["Success"] = false;
                            ResultDetails["Message"] = Resources.Replacement.Index.CannotDelete;
                        }
                    }
                }
                else
                {
                    ResultDetails["Success"] = false;
                    ResultDetails["Message"] = Resources.Replacement.Index.InvalidReplacement;
                }
            }
            else
            {
                ResultDetails["Success"] = false;
            }

            JsonResponse.Data = ResultDetails;

            return JsonResponse;
        }

        public JsonResult GetReplacementsByBusinessUnitIDByFilter(string filter)
        {
            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;
            IList<Replacement> replacements;

            replacements = replacementService.GetByBusinessUnitIDByFilter(CurrentBusinessUnitID, filter);

            return Json(replacements, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetReplacementByID(int id)
        {
            Replacement replacement = replacementService.GetReplacementByID(id);
            return Json(replacement, JsonRequestBehavior.AllowGet);
        }
    }
}